package org.acb.sgp.dao.impl;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.acb.sgp.beans.Distrito;
import org.acb.sgp.dao.DistritoDao;
import org.acb.sgp.factory.ConnectionFactory;

public class DistritoDaoImpl implements DistritoDao {

    private Connection con;
    private CallableStatement cs;
    private ResultSet rs;

    private Connection getConnection() throws SQLException {
        return ConnectionFactory.getInstance().getConnection();
    }

    @Override
    public void registrarDistrito(Distrito distrito) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void eliminarDistrito(Distrito distrito) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Distrito buscarDistritoPorCodigo(Integer codDistrito) {
        return null;
    }

    @Override
    public List<Distrito> listaDistritos() {
        List<Distrito> distritos = new ArrayList<Distrito>();
        try {
            String sql = "{Call ListarDistritos()}";
            con = getConnection();
            cs = con.prepareCall(sql);
            rs = cs.executeQuery();
            while (rs.next()) {
                distritos.add(llenar(rs));
            }
        } catch (SQLException ex) {
            Logger.getLogger(DistritoDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            ConnectionFactory.closing(rs, cs, con);
        }
        return distritos;
    }

    private Distrito llenar(ResultSet rs) throws SQLException {
        Distrito distrito = new Distrito();
        distrito.setCodDistrito(rs.getInt("codDistrito"));
        distrito.setNomDistrito(rs.getString("nomDistrito"));
        return distrito;
    }

}
